const { Op } = require('sequelize');
const userDao = require('../dao/UserDao');
const { crypto, format, database } = require('../utils/index');

var date = new Date();

const getCondition = param => {
  const regx = [
    /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/, // 匹配手机号
    /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/
  ];
  let condition = {};
  if (regx[0].test(param)) {
    // 匹配手机号
    condition = { user_phone: param };
  } else if (regx[1].test(param)) {
    // 匹配邮箱
    condition = { user_email: param };
  } else {
    // 匹配用户名
    condition = { user_name: param };
  }
  return condition;
};

async function login(account, password) {
  // await isConnection();
  const res = await userDao.findOne({ where: { user_phone: account } });
  if (res) {
    console.log(res.dataValues);
  } else {
    console.log('查无此人！');
  }
}

async function addUser(form) {
  const condition = getCondition(form['username']);
  const isExit = await userDao.findOne({ where: condition });
  if (isExit) {
    console.log('账号已存在');
  } else {
    const res = await userDao.create({
      ...condition,
      user_pwd: crypto.encrypt(form['password']),
      create_time: format('YYYY-mm-dd HH:MM:SS', date),
      update_time: format('YYYY-mm-dd HH:MM:SS', date)
    });
    if (res) {
      console.log('添加成功');
    } else {
      console.log('添加失败！');
    }
  }
}

async function deleteUser(condition) {
  const isExit = await userDao.findOne({ where: condition });
  if (isExit) {
    const res = await userDao.delete({ where: condition });
    if (res) {
      console.log(`成功删除${res}条数据.`);
    }
  } else {
    console.log('查无此人！');
  }
}

async function updateUser(obj) {
  const res = await userDao.update(obj);
  if (res) {
    console.log('修改成功');
  } else {
    console.log('修改失败');
  }
}

// async function findUser(condition) {
//   const res = await userDao.findAll(condition);
//   if (res) {
//     console.log(res);
//   } else {
//     console.log('查无数据！');
//   }
// }

// 该方法还支持模糊查询
async function list(conditions) {
  const res = await userDao.findAll(conditions);
  let userList = [];
  if (res) {
    res.forEach(element => {
      userList.push(element.dataValues);
    });
    console.log(userList);
  } else {
    console.log('查无数据！');
  }
}

// 该方法还支持模糊查询
async function listPage(conditions) {
  const { count, rows } = await userDao.findAndCountAll(conditions);
  console.log(count);
  console.log(rows);
}

async function test() {
  // await login('18339677543', '123456');

  // await addUser({
  //   account: '18237996844',
  //   password: '123456'
  // });

  // await deleteUser({ user_id: 3 });

  // await updateUser({
  //   fields: {
  //     user_name: 'andy',
  //     update_time: format('YYYY-mm-dd HH:MM:SS', date)
  //   },
  //   conditions: {
  //     where: {
  //       user_id: 4
  //     }
  //   }
  // });

  // await list({
  //   where: {
  //     user_name: {
  //       [Op.like]: '%l%'
  //     }
  //   }
  // });

  // await list();

  await listPage({
    // where: {
    //   user_name: {
    //     [Op.like]: '%l%'
    //   }
    // },
    offset: 0,
    limit: 5
  });

  await database.close();
}

test();
